package org.openclinic.service.impl;

import java.util.List;

import org.openclinic.domain.DocumentModel;
import org.openclinic.exception.DomainException;
import org.openclinic.exception.ErrorCodes;
import org.openclinic.repository.DocumentModelRepository;
import org.openclinic.service.DocumentModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

@Service
public class DocumentModelServiceImpl implements DocumentModelService {
	
	@Autowired
	private DocumentModelRepository documentModelRepository;
	
	@Override
	public void delete(long id) {
		documentModelRepository.delete(id);
	}
	
	@Override
	public DocumentModel find(long id) {
		return documentModelRepository.findOne(id);
	}
	
	@Override
	public List<DocumentModel> list() {
		return documentModelRepository.findAll();
	}
	
	@Override
	public DocumentModel save(DocumentModel documentModel) {
		try {
			return documentModelRepository.save(documentModel);
		} catch (DataIntegrityViolationException e) {
			throw new DomainException("documentModel", "name", ErrorCodes.UNIQUE_VIOLATED, documentModel.getName());
		}
	}
}
